
import { Component, OnInit } from '@angular/core';
import 'rxjs/add/operator/map';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { UserModel } from '../../model/sys/user-model';
import { DateUtils } from '../../model/comm/date-utils';
import { Notifycation } from 'app/ptpmc/comm/notifycation';

export class MenuItem {
    parentId: string;
    id: string;
    parentIds: string;
    name: string;
    href: string;
    target: string;
    icon: string;
    sort: number;
    isShow: string;
    permission: string;
    createBy: string;
    createDate: string;
    updateBy: string;
    updateDate: string;
    remarks: string;
    delFlag: string;

}

@Component({
    selector: 'app-report-user',
    templateUrl: './menu.component.html',
    styleUrls: ['./menu.component.scss']
})

export class MenuComponent implements OnInit {
    title = '系统菜单管理';
    dialogTitle = '添加菜单';
    menus: any[] = [{ name: '待加载数据...', }];
    currentItem: any;
    currentMenuItem: MenuItem;
    roleList: any[];
    menuRoles: any[] = [];
    popupVisible = false;
    editDialogVisible = false;

    menuItem: MenuItem = {
        parentId: '0',
        id: '',
        parentIds: '',
        name: '',
        href: '',
        target: '',
        icon: '',
        sort: 1,
        isShow: '1',
        permission: '',
        createBy: '1',
        createDate: '2018-03-14 00:00:00',
        updateBy: '1',
        updateDate: '2018-03-15 00:00:00',
        remarks: '',
        delFlag: '0',
    };

    constructor(private commService: CommService) {
    }

    ngOnInit() {
        this.refreshMenu();
        this.currentItem = this.menus[0];
    }

    refreshMenu() {
        this.getMenuList();
        this.getRoleList();
    }

    getMenuList(): void {
        this.commService.doGet(ReportUrls.menus)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.menus = [];
                    model.data.forEach(menu => {
                        const menuItemRoot: { id, name, href, expanded }
                            = { id: menu.id, name: menu.name, href: menu.href, expanded: true };
                        const menuItem: { id, name, href, expanded, categoryId }
                            = { id: menu.id, name: menu.name, href: menu.href, expanded: true, categoryId: menu.parentId };
                        if (menu.id === menu.parentId) {
                            this.menus.push(menuItemRoot);
                        } else {
                            this.menus.push(menuItem);
                        }
                    });
                    this.currentItem = this.menus[0];
                }
            });
    }

    getRoleList() {
        this.commService
            .getList({}, ReportUrls.AllUser)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.roleList = model.data.allRole;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }

    selectItem(item: any) {
        this.currentItem = item.itemData;
        this.getRolesOnMenuId();
    }
    getRolesOnMenuId() {
        this.commService.doGet(ReportUrls.menuRoles + '/' + this.currentItem.id)
            .subscribe(model => {
                this.menuRoles = [];
                if (model.resultCode === 200) {
                    this.menuRoles = model.data;
                    this.getRoleList();
                }
            });
    }

    roleIsChecked(role: any) {
        if (this.menuRoles === undefined) { return false; }
        return this.menuRoles.find(e => e.roleId === role.id) !== undefined;
    }

    bindRoleToMenu(role: any) {
        this.commService.doGet(ReportUrls.menus + '/' + role.id + '/' + this.currentItem.id).subscribe(model => {
            Notifycation.showNotification(model.resultCode === 200 ? '更新成功' : '更新失败', 'info');
        });
    }
    canceLAddMenu() {
        this.popupVisible = false;
        this.editDialogVisible = false;
    }
    saveMenuItem() {

        if (this.menuItem.href === '' || this.menuItem.name === '' || this.menuItem.id === '') {
            Notifycation.showNotification('请填写完整表单数据', 'danger');
            return;
        }
        this.menuItem.parentId = this.currentItem.id;
        this.menuItem.parentIds = this.currentItem.id;
        this.menuItem.createBy = UserModel.userId;
        this.menuItem.createDate = DateUtils.getNowFormatDate();
        this.menuItem.updateBy = UserModel.userId;
        this.menuItem.updateDate = DateUtils.getNowFormatDate();

        this.commService
            .getList(this.menuItem, ReportUrls.menuItemPut)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    Notifycation.showNotification('添加成功!', 'success');
                    this.popupVisible = false;
                    this.refreshMenu();
                } else {
                    Notifycation.showNotification('添加失败！', 'danger');
                }
            });
    }

    editMenu() {
        this.commService.doGet(ReportUrls.menus + '/' + this.currentItem.id).subscribe(model => {
            if (model.resultCode === 200) {
                this.currentMenuItem = model.data;
                this.editDialogVisible = true;
            } else {
                Notifycation.showNotification('菜单数据获取失败', 'danger');
            }
        });
    }
    saveEditMenuItem() {
        if (this.currentMenuItem.href === '' || this.currentMenuItem.name === '' || this.currentMenuItem.id === '') {
            Notifycation.showNotification('请填写完整表单数据', 'danger');
            return;
        }
        this.currentMenuItem.updateBy = UserModel.userId;
        this.currentMenuItem.updateDate = DateUtils.getNowFormatDate();

        this.commService
            .getList(this.currentMenuItem, ReportUrls.menuItemEdit)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    Notifycation.showNotification('修改成功!', 'success');
                    this.editDialogVisible = false;
                    this.refreshMenu();
                } else {
                    Notifycation.showNotification('修改失败！', 'danger');
                }
            });
    }

    delMenu() {
        if (confirm('确认删除[' + this.currentItem.name + ']?')) {
            this.commService.doGet(ReportUrls.menuDel + '/' + this.currentItem.id).subscribe(model => {
                Notifycation.showNotification(model.resultCode === 200 ? '更新成功' : '更新失败', 'info');
                this.refreshMenu();
            });
        }
    }

    setValue(type: any) {
        if (type === 1) {
            this.menuItem.isShow = (this.menuItem.isShow === '1' ? '0' : '1');
        } else {
            this.currentMenuItem.isShow = (this.currentMenuItem.isShow === '1' ? '0' : '1');
        }
    }

}
